﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using ColorApplication.Helper;
using System.Drawing;
using System.Configuration;

namespace ColorApplication.color
{
    public partial class ColorsAttribute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }

            try
            {
                // If ColorId is Pass Set value for Data Source of grid view
                if (Request.QueryString["ColorId"] != null)
                {
                    if (Request.QueryString["ColorId"].ToString() != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(Request.QueryString["ColorId"].ToString());
                        ColorAttributegridSqlDataSource.SelectParameters.Clear();

                        Parameter Idparemeter = new Parameter("Id");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Idparemeter);

                        Parameter Colorparameter = new Parameter("ColorId", DbType.Int32, ColorId.ToString());
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Colorparameter);

                        Parameter Attributeparameter = new Parameter("AttributeId");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Attributeparameter);

                        Parameter BrightnessLevelFromparameter = new Parameter("BrightnessLevelFrom");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelFromparameter);

                        Parameter BrightnessLevelToparameter = new Parameter("BrightnessLevelTo");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelToparameter);

                        Parameter BrightnessMaxValueparameter = new Parameter("BrightnessMaxValue");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessMaxValueparameter);
                    }
                }
                else if (Request.QueryString["AttributeId"] != null)
                {
                    if (Request.QueryString["AttributeId"].ToString() != string.Empty)
                    {
                        int AttributeId = Convert.ToInt32(Request.QueryString["AttributeId"].ToString());
                        ColorAttributegridSqlDataSource.SelectParameters.Clear();

                        Parameter Idparemeter = new Parameter("Id");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Idparemeter);

                        Parameter Colorparameter = new Parameter("ColorId");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Colorparameter);

                        Parameter Attributeparameter = new Parameter("AttributeId", DbType.Int32, AttributeId.ToString());
                        ColorAttributegridSqlDataSource.SelectParameters.Add(Attributeparameter);

                        Parameter BrightnessLevelFromparameter = new Parameter("BrightnessLevelFrom");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelFromparameter);

                        Parameter BrightnessLevelToparameter = new Parameter("BrightnessLevelTo");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelToparameter);

                        Parameter BrightnessMaxValueparameter = new Parameter("BrightnessMaxValue");
                        ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessMaxValueparameter);
                    }
                }
                else
                {
                    // If Next Time remove ColorId value for Data Source of grid view

                    ColorAttributegridSqlDataSource.SelectParameters.Clear();

                    Parameter Idparemeter = new Parameter("Id");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(Idparemeter);

                    Parameter Colorparameter = new Parameter("ColorId");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(Colorparameter);

                    Parameter Attributeparameter = new Parameter("AttributeId");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(Attributeparameter);

                    Parameter BrightnessLevelFromparameter = new Parameter("BrightnessLevelFrom");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelFromparameter);

                    Parameter BrightnessLevelToparameter = new Parameter("BrightnessLevelTo");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessLevelToparameter);

                    Parameter BrightnessMaxValueparameter = new Parameter("BrightnessMaxValue");
                    ColorAttributegridSqlDataSource.SelectParameters.Add(BrightnessMaxValueparameter);
                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();

            }

        }
        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }
        protected void ColorAttributeGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Change Deatils view Mode to edit
                ColorAttributeDetailsView.ChangeMode(DetailsViewMode.Edit);
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void ColorAttributeGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            try
            {
                // Bind java Script for Are you Sure to Delete Confirm Box
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton delinkbutton = (LinkButton)e.Row.FindControl("DeleteLinkButton");
                    delinkbutton.OnClientClick = "return getDeleteMessage()";


                    // In Grid view Show Color according to RGB value
                    int ColorId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ColorId"));

                    TextBox ShowColorTextBox = (TextBox)e.Row.FindControl("ShowColorTextBox");
                    // Pass ColorId for get Respective Color
                    ShowColorTextBox.BackColor = getColor(ColorId);

                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }
        /// <summary>
        /// getColor function accept colorId as parameter and return respective color
        /// </summary>
        /// <param name="ColorId"></param>
        /// <returns></returns>
        public Color getColor(int ColorId)
        {
            Color col = Color.FromArgb(0, 0, 0);
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameter = { new SqlParameter("@Id", ColorId) };

                SqlCommand cmd = SQLHelper.GetCommand(con, "GetColorsSelect", parameter);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();

                if (ds.Tables[0].Rows.Count > 0)
                {
                    int RCode = Convert.ToInt32(ds.Tables[0].Rows[0]["Rcode"].ToString());
                    int GCode = Convert.ToInt32(ds.Tables[0].Rows[0]["Gcode"].ToString());
                    int BCode = Convert.ToInt32(ds.Tables[0].Rows[0]["Bcode"].ToString());
                    col = Color.FromArgb(RCode, GCode, BCode);
                }
            }
            return col;
        }
        protected void ColorAttributeDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record updated successfully.";
                ColorAttributeDetailsView.ChangeMode(DetailsViewMode.Insert);
                ColorAttributeGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void ColorAttributeDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record inserted successfully.";
                ColorAttributeDetailsView.ChangeMode(DetailsViewMode.Insert);
                ColorAttributeGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void ColorAttributeDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                int OldColorId = Convert.ToInt32(e.OldValues["ColorId"].ToString());
                int OldAttributeId = Convert.ToInt32(e.OldValues["AttributeId"].ToString());

                int NewColorId = Convert.ToInt32(e.NewValues["ColorId"].ToString());
                int NewAttributeId = Convert.ToInt32(e.NewValues["AttributeId"].ToString());

                if (OldColorId != NewColorId || OldAttributeId != NewAttributeId)
                {
                    //  This Function Check Record in Attribute Family for Dublicate code name exist or Not
                    if (CheckColorAttributeExistinColorAttribute(NewColorId, NewAttributeId) == true)
                    {
                        ErrorLabel.Text = "Color Attribute already exists. Duplicate record is not allowed.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void ColorAttributeDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            try
            {
                int ColorId = Convert.ToInt32(e.Values["ColorId"].ToString());
                int AttributeId = Convert.ToInt32(e.Values["AttributeId"].ToString());
                //  This Function Check Record in Attribute Family for Dublicate code name exist or Not
                if (CheckColorAttributeExistinColorAttribute(ColorId, AttributeId) == true)
                {
                    ErrorLabel.Text = "Color Attribute already exists. Duplicate record is not allowed.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }


        public bool CheckColorAttributeExistinColorAttribute(int ColorId, int AttributeId)
        {
            // This Function Check Record in  ColorAttribute Table For Dublicate record Exist or Not
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = { new SqlParameter("@ColorId", ColorId),
                                              new SqlParameter("@AttributeId",AttributeId) 
                                            };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckColorAttributeExistinColorAttribute", parameters);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;
            }

            return false;

        }
        /// <summary>
        /// FindAndColor function set background color accoding to details view mode
        /// </summary>
        public void FindAndColor()
        {
            // This function find Color Id and display color accroding to textbox back ground color
            if (ColorAttributeDetailsView.CurrentMode == DetailsViewMode.Insert)
            {
                // When Mode is Insert 
                DropDownList InsertColorDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertColorDropDownList");
                TextBox InsertShowColorTextBox = (TextBox)ColorAttributeDetailsView.FindControl("InsertShowColorTextBox");
                if (InsertColorDropDownList != null && InsertShowColorTextBox != null)
                {
                    if (InsertColorDropDownList.SelectedValue != null && InsertColorDropDownList.SelectedValue != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(InsertColorDropDownList.SelectedValue);
                        InsertShowColorTextBox.BackColor = getColor(ColorId);
                    }
                }

            }

            if (ColorAttributeDetailsView.CurrentMode == DetailsViewMode.Edit)
            {
                // When Mode is Edit
                DropDownList EditColorDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("EditColorDropDownList");
                TextBox EditShowColorTextBox = (TextBox)ColorAttributeDetailsView.FindControl("EditShowColorTextBox");

                if (EditColorDropDownList != null && EditShowColorTextBox != null)
                {
                    if (EditColorDropDownList.SelectedValue != null && EditColorDropDownList.SelectedValue != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(EditColorDropDownList.SelectedValue);
                        EditShowColorTextBox.BackColor = getColor(ColorId);
                    }
                }
            }
        }

        protected void InsertColorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void EditColorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void ColorAttributeDetailsView_DataBound(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertAttributeDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["AttributeId"] != null)
                {
                    if (Request.QueryString["AttributeId"].ToString() != string.Empty)
                    {
                        int AttributeId = Convert.ToInt32(Request.QueryString["AttributeId"].ToString());
                        DropDownList InsertAttributeDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertAttributeDropDownList");
                        if (InsertAttributeDropDownList != null)
                        {
                            InsertAttributeDropDownList.SelectedValue = AttributeId.ToString();
                        }

                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertColorDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["ColorId"] != null)
                {
                    if (Request.QueryString["ColorId"].ToString() != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(Request.QueryString["ColorId"].ToString());
                        DropDownList InsertColorDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertColorDropDownList");
                        if (InsertColorDropDownList != null)
                        {
                            InsertColorDropDownList.SelectedValue = ColorId.ToString();
                            FindAndColor();
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void EditBrightnessLevelFromDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertBrightnessLevelFromDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void EditBrightnessLevelToDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertBrightnessLevelToDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void EditBrightnessMaxValueDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Find Color in DropDown And Display According to User
                FindAndColor();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertBrightnessMaxValueDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void InsertBrightnessLevelFromDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["BrightnessLevelFrom"] != null)
                {
                    if (Request.QueryString["BrightnessLevelFrom"].ToString() != string.Empty)
                    {
                        double ColorId = Convert.ToDouble(Request.QueryString["BrightnessLevelFrom"].ToString());
                        DropDownList InsertBrightnessLevelFromDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertBrightnessLevelFromDropDownList");
                        if (InsertBrightnessLevelFromDropDownList != null)
                        {
                            InsertBrightnessLevelFromDropDownList.SelectedValue = ColorId.ToString();
                            FindAndColor();
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertBrightnessLevelToDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["BrightnessLevelTo"] != null)
                {
                    if (Request.QueryString["BrightnessLevelTo"].ToString() != string.Empty)
                    {
                        double ColorId = Convert.ToDouble(Request.QueryString["BrightnessLevelTo"].ToString());
                        DropDownList InsertBrightnessLevelToDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertBrightnessLevelToDropDownList");
                        if (InsertBrightnessLevelToDropDownList != null)
                        {
                            InsertBrightnessLevelToDropDownList.SelectedValue = ColorId.ToString();
                            FindAndColor();
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertBrightnessMaxValueDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["BrightnessMaxValue"] != null)
                {
                    if (Request.QueryString["BrightnessMaxValue"].ToString() != string.Empty)
                    {
                        double ColorId = Convert.ToDouble(Request.QueryString["BrightnessMaxValue"].ToString());
                        DropDownList InsertBrightnessMaxValueDropDownList = (DropDownList)ColorAttributeDetailsView.FindControl("InsertBrightnessMaxValueDropDownList");
                        if (InsertBrightnessMaxValueDropDownList != null)
                        {
                            InsertBrightnessMaxValueDropDownList.SelectedValue = ColorId.ToString();
                            FindAndColor();
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }
    }
}